package com.hymjweb.demo.hymj.department_manage.service;

import com.hymjweb.demo.framework.cache.service.GlobalCacheService;
import com.hymjweb.demo.framework.result.MIniUIGridResultMap;
import com.hymjweb.demo.framework.sys.BaseService;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.department_manage.bean.XtDepartmentCachePojo;
import com.hymjweb.demo.hymj.department_manage.bean.XtDepartmentPojo;
import com.hymjweb.demo.hymj.department_manage.mapper.XtDepartmentMapper;
import com.hymjweb.demo.hymj.log_manage.aop.annotation.OperationLogger;
import com.hymjweb.demo.hymj.system_maintenance.user.bean.XtUserPojo;
import com.hymjweb.demo.hymj.system_maintenance.user.service.XtUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class XtDepartmentService extends BaseService {
    @Autowired
    private XtDepartmentMapper xtDepartmentMapper;

    @Autowired
    private XtUserService xtUserService;

    @Autowired
    private GlobalCacheService globalCacheService;

    public List<XtDepartmentCachePojo> findCacheAll() {
        List<XtDepartmentCachePojo> pojos = xtDepartmentMapper.findCacheAll();
        return pojos;
    }

    public Object getPageList(Map map) {
        this.pageListInit(map);
        List list = xtDepartmentMapper.findAll(map);
        MIniUIGridResultMap resultMap = this.pageToGrid(list);
        return resultMap;
    }

    /**
     * 根据领导id查询部门集合
     *
     * @param userId
     * @return
     */
    public List<XtDepartmentPojo> findByLeaderId(String userId) {
        if(DataTypeUtil.isEmptyStr(userId)){
            return null;
        }
        return xtDepartmentMapper.findByLeaderId(userId);
    }

    /**
     * 根据用户id获取用户部门对象
     *
     * @param userId
     * @return
     */
    public XtDepartmentPojo getDepartmentPojoByUserId(String userId) {
        if(DataTypeUtil.isEmptyStr(userId)){
            return null;
        }
        return xtDepartmentMapper.getDepartmentByUserId(userId);
    }

    /**
     * 根据条件加载部门树
     *
     * @param param
     * @return
     */
    public Object treeQuery(Map param) {
        return xtDepartmentMapper.treeQuery(param);
    }

    // @OperationLogger(oper = SysConstant.OPER_ADD, operTitle = "部门-添加", operMemo = "", classType = XtDepartmentPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int insert(XtDepartmentPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("部门添加参数错误!");
        }
        Date currentDate = this.getSysTime();
        String czyId = this.getLoginCzyId();
        pojo.setId(this.getSysStrId());
        pojo.setCreateTime(currentDate);
        pojo.setCreateUserId(czyId);
        pojo.setUpdateTime(currentDate);
        pojo.setUpdateUserId(czyId);

        int affectRows = xtDepartmentMapper.insert(pojo);

        if (affectRows > SysConstant.INT_ZERO) {
            // 重新加载部门缓存
            globalCacheService.loadDepartmentCache();
        }
        return affectRows;
    }

    @OperationLogger(oper = SysConstant.OPER_UPDATE, operTitle = "部门-更新", operMemo = "", classType = XtDepartmentPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int update(XtDepartmentPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("部门更新参数错误!");
        }

        pojo.setUpdateTime(this.getSysTime());
        pojo.setUpdateUserId(this.getLoginCzyId());

        int affectRows = xtDepartmentMapper.update(pojo);

        if (affectRows > SysConstant.INT_ZERO) {
            // 重新加载部门缓存
            globalCacheService.loadDepartmentCache();
        }
        return affectRows;
    }

    // @OperationLogger(oper = SysConstant.OPER_DELETE, operTitle = "部门-删除", operMemo = "", classType = XtDepartmentPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int delete(String id) throws Exception {
        if (DataTypeUtil.isEmptyStr(id)) {
            throwException("部门删除参数错误!");
        }

        //删除部门时，如果该部门下有员工，不允许删除
        List<XtUserPojo> xtUserPojoList = xtUserService.findByDepartmentId(id);

        if (xtUserPojoList != null && !xtUserPojoList.isEmpty()) {
            throwException("待删除部门下有员工存在，不能删除部门!");
        }
        int affectRows = xtDepartmentMapper.delete(id);
        if (affectRows > SysConstant.INT_ZERO) {
            // 重新加载部门缓存
            globalCacheService.loadDepartmentCache();
        }
        return affectRows;
    }


}
